package com.nofeng.wx.controller.stock;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nofeng.wx.controller.stock.view.*;
import com.nofeng.wx.service.FindStockService;
import com.nofeng.wx.service.StockService;
import com.nofeng.wx.service.stock.BacktestResult;
import com.nofeng.wx.service.stock.DailyStockData;
import com.nofeng.wx.third.EastMoneyStockApi;
import com.nofeng.wx.third.FinanceHttpApi;
import com.nofeng.wx.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.nofeng.wx.service.stock.StrategyBacktester.runBacktest;

@RestController
@CrossOrigin(origins = "*")
@Slf4j
public class StockController {

    @Autowired
    StockService stockService;

    @Autowired
    FindStockService findStockService;

    @Autowired
    FinanceHttpApi financeHttpApi;

    @RequestMapping(value = "/stock/ai/{code}")
    public String ai(@PathVariable String code) {
        String resp = null;
        try {
            resp = AIUtils.ai(code);
        } catch (Exception e) {
            e.printStackTrace();
            resp="{}";
        }
        return resp;
    }

    @RequestMapping(value = "/stock/news")
    public String news() {
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("news",financeHttpApi.news(3));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject.toJSONString();
    }

    @RequestMapping(value = "/stock/result/{code}")
    public String result(@PathVariable String code){
        return JSONObject.toJSONString(stockService.toDayResult(code));

    }

    @RequestMapping(value = "/stock/refreshWg")
    public String refreshWg(){
        stockService.refreshWg();
        return "刷新成功";

    }

    @RequestMapping(value = "/stock/refreshFundFlow")
    public String refreshFundFlow(){
        try {
            findStockService.refreshFundFlow();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "刷新成功";
    }

    @RequestMapping(value = "/stock/refresh")
    public String refresh(){
        try {
            findStockService.refresh();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "刷新成功";
    }

    @RequestMapping(value = "/stock/findStrongStock")
    public String findStrongStock(){
        try {
            List<JSONObject> strongStock = findStockService.findStrongStock();
            return JSONObject.toJSONString(strongStock,true);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping(value = "/stock/saveStrongStock")
    public String saveStrongStock(){
        try {
            findStockService.saveStrongStock();
            findStockService.refresh();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "刷新成功";
    }


    @RequestMapping(value = "/stock/bk/{code}")
    public String bk(@PathVariable String code) {
        JSONArray stockArray = new JSONArray();
        JSONArray result = new JSONArray();
        try {
            stockArray = EastMoneyStockApi.getHotIndustryDetail(code);
            for (int i = 0; i < stockArray.size(); i++) {
                JSONObject s = stockArray.getJSONObject(i);
                String scode = s.getString("f12");
                String stock = stockService.stock(scode, false,65);
                JSONObject stockJSON=JSONObject.parseObject(stock);
                FinanceHttpApi.HttpStockData today = stockJSON.getObject("stock", FinanceHttpApi.HttpStockData.class);
                s.put("todayOverMA20",today.getOverMa20());
                if(today.getOverMa20()) {
                    s.put("f14", s.getString("f14") + "【20天均线】");
                }
                result.add(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toJSONString();
    }

    @RequestMapping(value = "/stock/stock/{code}")
    public String stock(@PathVariable String code) {
        return stockService.stock(code,true);
    }

    @RequestMapping(value = "/stock/stockApi")
    public String stockApi(@RequestParam String code) {
        return stockService.stock(code,true);
    }

    @RequestMapping(value = "/stock/stockName/{code}")
    public String stockName(@PathVariable String code) {
        try {
            List<FinanceHttpApi.HttpStockData> stockData = FinanceHttpApi.getNow(code);
            return JSON.toJSONString(stockData.get(0));
        } catch (Exception e) {
            return "{}";
        }
    }

    @RequestMapping(value = "/stock/today")
    public String today() {
        return JSONObject.toJSONString(GuavaCacheUtils.nbList);
    }

    @RequestMapping(value = "/stock/f5")
    public String f5() {
        stockService.refreshPower();
        return "running";
    }



    @RequestMapping(value = "/stock/onHandForCode/{code}")
    public OnHandView onHandForCode(@PathVariable String code) {
        OnHandView onHandView=new OnHandView();
        try {
            List<String> codeList = Lists.newArrayList(code.split(","));
            onHandView = stockService.onHand(codeList,240);
        } catch (Exception e) {
            log.error("error", e);
        }
        return onHandView;
    }

    @RequestMapping(value = "/stock/onHandForCode15/{code}")
    public OnHandView onHandForCode15(@PathVariable String code) {
        OnHandView onHandView=new OnHandView();
        try {
            List<String> codeList = Lists.newArrayList(code.split(","));
            onHandView = stockService.onHand(codeList,15);
        } catch (Exception e) {
            log.error("error", e);
        }
        return onHandView;
    }

    @RequestMapping(value = "/stock/onHand")
    public OnHandView onHand() {
        OnHandView onHandView=new OnHandView();
        try {
            List<String> codeList = Lists.newArrayList();
            onHandView = stockService.onHand(codeList,60);
        } catch (Exception e) {
            log.error("error", e);
        }
        return onHandView;
    }

    @RequestMapping(value = "/stock/onHand15")
    public OnHandView onHand15() {
        OnHandView onHandView=new OnHandView();
        try {
            List<String> codeList = Lists.newArrayList();
            onHandView = stockService.onHand(codeList,15);
        } catch (Exception e) {
            log.error("error", e);
        }
        return onHandView;
    }

    @RequestMapping(value = "/stock/rsi/{code}")
    public StockReportRsiView rsi(@PathVariable String code) {
        StockReportRsiView rsiView = new StockReportRsiView();
        try {
            rsiView = stockService.analysisRsi(code);
        } catch (Exception e) {
            log.error("error", e);
        }
        return rsiView;
    }

    @RequestMapping(value = "/stock/minuteRsi/{code}")
    public StockReportMinuteRsiView minuteRsi(@PathVariable String code) {
        StockReportMinuteRsiView rsiView = new StockReportMinuteRsiView();
        try {
            rsiView = stockService.analysisMinuteRsi(code,240);
        } catch (Exception e) {
            log.error("error", e);
        }
        return rsiView;
    }

    @RequestMapping(value = "/stock/minuteRsi15/{code}")
    public StockReportMinuteRsiView minuteRsi15(@PathVariable String code) {
        StockReportMinuteRsiView rsiView = new StockReportMinuteRsiView();
        try {
            rsiView = stockService.analysisMinuteRsi(code,15);
        } catch (Exception e) {
            log.error("error", e);
        }
        return rsiView;
    }

    @RequestMapping(value = "/stock/list/{code}")
    public List<DailyStockData> list(@PathVariable String code) {
        List<String> codeList = Lists.newArrayList(code.split(","));
        List<DailyStockData> result = Lists.newArrayList();
        try {
            result = stockService.sortRsi(codeList,60);
        } catch (Exception e) {
            log.error("error", e);
        }
        return result;
    }

    @RequestMapping(value = "/stock/listEncrypt/{code}")
    public String listEncrypt(@PathVariable String code) {
        List<String> codeList = Lists.newArrayList(code.split(","));
        List<DailyStockData> result = Lists.newArrayList();
        try {
            result = stockService.sortRsi(codeList,60);
        } catch (Exception e) {
            log.error("error", e);
        }
        try {
            return  AesUtils.encrypt(JSONObject.toJSONString(result),"46cc793c53dc451b");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    @RequestMapping(value = "/stock/todayView")
    public TodayView todayView() {
        TodayView todayView = new TodayView();
        try {
            todayView.setSz(stockService.sz());
            todayView.setBtf(stockService.bft());
            Integer expectOnHandP = (110 - todayView.getBtf().intValue()) * 2;
            todayView.setExpectOnHandP(expectOnHandP);
            todayView.setExpctOnHandMoney(BigDecimal.valueOf(expectOnHandP * 0.01 * 100));
//            FinanceHttpApi.HttpStockData usdcnh = stockService.getUSDCNH();
//            if(null!=usdcnh){
//                todayView.setUsdcnh(usdcnh.getClose());
//                todayView.setUsdcnhPreClose(usdcnh.getOpen());
//            }

        } catch (Exception e) {
            log.error("error", e);
        }
        return todayView;
    }
}
